//
//  CMDBAlbumsViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/20.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let KRightWith = 0.8 * CMDB_Screen_Width

private let kHeight    = KRightWith * 2 / 3

class CMDBAlbumsViewController: CMDBBaseViewController {
    
    var headerView : UIView!
    
    var contentView : UIScrollView!
    
    var bottomView : UIView!
    
    var albumNames : [String] = ["旅游相册","个人xiangce"]
    
    var checkImage : UIImageView!
    
    var selectedBtn : UIButton?

    override func setup() {
        super.setup()
        
        buildSubViews()
    }
}

extension CMDBAlbumsViewController{
    
    func buildSubViews(){
        
        buildHeaderView()
        
        buildContentView()
        
        buildBottomView()
        
    }
    
    
    func buildHeaderView(){
        
        headerView       = UIView.getInstance()
        
        headerView.frame = CGRect(x: 0, y: 0, width: KRightWith, height: kHeight + 90 * CMDB_Rate)
        
        let bgImageView  = UIImageView.getInstance()
        
        bgImageView.image = UIImage(named: "相册背景图")
        
        bgImageView.frame = CGRect(x: 0, y: 0, width: KRightWith, height: kHeight)
        
        headerView.addSubview(bgImageView)

        let addAlbumBtn = UIButton()
        
        addAlbumBtn.setBackgroundImage(UIImage(named:"上传照片-添加照片" ), forState: .Normal)
        
        addAlbumBtn.addTarget(self, action: #selector(addAlbumClick), forControlEvents: .TouchUpInside)
        
        addAlbumBtn.frame = CGRect(x: 0, y: 0, width: 200 * CMDB_Rate, height: 200 * CMDB_Rate)
        
        addAlbumBtn.center = headerView.center
        
        let showAlbumView = UIView.getInstance()
        
        let showAlbumImageView  = UIImageView(frame: CGRect(x: 15, y: 12, width: 25, height: 21))
        
        showAlbumImageView.image =  UIImage(named: "相册icon")
        
        let showAlbumLabel = UILabel.getInstance()
        
        showAlbumLabel.text = "选择相册"
        
        showAlbumLabel.font = CMDB_13_FONT
        
        showAlbumLabel.textColor = word87Color
        
        showAlbumLabel.sizeToFit()
        
        showAlbumView.addSubview(showAlbumImageView)
        
        showAlbumView.addSubview(showAlbumLabel)
        
        showAlbumLabel.snp_makeConstraints { (make) in
            make.left.equalTo(showAlbumImageView.snp_right).offset(15);
            make.top.equalTo(16)
            make.height.equalTo(showAlbumLabel.height);
            make.width.equalTo(showAlbumLabel.width)
        }
        
        headerView.addSubview(addAlbumBtn)
        
        headerView.addSubview(showAlbumView)
        
        showAlbumView.snp_makeConstraints { (make) in
            make.left.equalTo(0);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(90 * CMDB_Rate)
        }
        
        view.addSubview(headerView)
    }
    
    func buildContentView(){
        
        contentView = UIScrollView.getInstance()
        
        for (index,albumName) in albumNames.enumerate() {
            
            let albunButton = UIButton.getInstance()
            
            albunButton.addTarget(self, action: #selector(selectedAlbumClick(_:)), forControlEvents: .TouchUpInside)
            
            albunButton.frame = CGRect(x: 0, y: CGFloat(88) * CMDB_Rate * CGFloat(index), width: KRightWith, height:CGFloat(88) * CMDB_Rate )
            
            albunButton.backgroundColor = UIColor.whiteColor()
            
            albunButton.selected = false
            
            albunButton.setTitle(albumName, forState: .Normal)
            
            albunButton.setTitleColor(word87Color, forState: .Normal)
            
            albunButton.titleLabel?.font = CMDB_13_FONT
            
            contentView.addSubview(albunButton)
            
            albunButton.contentHorizontalAlignment = .Left;
            
            albunButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            
            albunButton.tag = index
            
            if index == 0 {
                albunButton.selected = true
                albunButton.backgroundColor = UIColor.Hex(0xf2f2f2)
                selectedBtn = albunButton
            }
        }
        
        checkImage = UIImageView.getInstance()
        
        checkImage.image = UIImage(named: "完成")
        
        contentView.addSubview(checkImage)
        
        checkImage.snp_makeConstraints { (make) in
            make.right.equalTo(0);
            make.top.equalTo(15);
            make.width.equalTo(20);
            make.height.equalTo(20);
            
        }
        
        contentView.contentSize = CGSize(width: 0, height: CGFloat(albumNames.count) * CGFloat(88) * CMDB_Rate)
        
        contentView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.headerView.frame), width: KRightWith, height: CMDB_Screen_Height - kHeight + 90 * CMDB_Rate - 50 * CMDB_Rate)
        
        view.addSubview(contentView)
    }
    
    func buildBottomView(){
        
        bottomView = UIView.getInstance()
        
        
        view.addSubview(bottomView)
        
        bottomView.snp_makeConstraints { (make) in
            make.left.equalTo(0);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(100 * CMDB_Rate);
        }
        
        let upLoadBtn = UIButton.getInstance()
        
        upLoadBtn.setTitleColor(word87Color, forState: .Normal)
        
        upLoadBtn.addTarget(self, action: #selector(uploadClick), forControlEvents: .TouchUpInside)
        
        upLoadBtn.setTitle("确定上传", forState: .Normal)
        
        upLoadBtn.frame = bottomView.bounds
        
        bottomView.addSubview(upLoadBtn)
    }
    
}

//MARK: - action method
extension CMDBAlbumsViewController{

    @objc func addAlbumClick(){
        printLog("新建相册")
    }
    
    @objc func uploadClick(){
        
        printLog("上chuang")
        
    }
    
    @objc func selectedAlbumClick(sender : UIButton){
        
        selectedBtn?.backgroundColor = UIColor.whiteColor()
        
        selectedBtn!.selected = !selectedBtn!.selected
        
        sender.selected = !sender.selected
        
        checkImage.y = CGFloat(sender.tag) * CGFloat(44) + 15
        
        selectedBtn = sender
        
        printLog("选择相册")
        
    }
}
